//
//  ANTabBarController.m
//  Ants
//
//  Created by ZHM on 16/3/10.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANTabBarController.h"
#import "UIImage+image.h"
#import "ANTabBar.h"
#import "ANNavigationVC.h"
#import "ANHomeViewController.h"
#import "ANDiscoverViewController.h"
#import "ANProfileViewController.h"
#import "ANShoppingCarViewController.h"
#import "ANUserLoginViewController.h"

@interface ANTabBarController ()<ANTabBarDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ANTabBarController

- (NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加所有子控制器
    [self setUpAllChildViewController];
    self.tabBar.backgroundColor = [UIColor clearColor];
    //自定义TABBAR
    [self setUpTabBar];
}

- (void)setUpAllChildViewController {
    // 首页
    ANHomeViewController *home = [[ANHomeViewController alloc] init];
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"];
    // 发现
    ANDiscoverViewController *discover = [[ANDiscoverViewController alloc] init];
    [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"];
    
    // 购物车
    ANShoppingCarViewController *shoppingCar = [[ANShoppingCarViewController alloc] init];
    [self setUpOneChildViewController:shoppingCar image:[UIImage imageNamed:@"tabbar_shoppingcart"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_shoppingcart_selected"] title:@"购物车"];
    
    // 我
    ANProfileViewController *profile = [[ANProfileViewController alloc] init];
    [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"];
}

#pragma mark - 添加一个子控制器
- (void)setUpOneChildViewController:(UIViewController *)vc
                              image:(UIImage *)image
                      selectedImage:(UIImage *)selectedImage
                              title:(NSString *)title{
    vc.tabBarItem.image = image;
    vc.title = title;
    vc.tabBarItem.selectedImage = selectedImage;
    //保存tabBarItem模型数据
    [self.items addObject:vc.tabBarItem];
    //设置导航控制器的根控制器
    ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

#pragma mark - 设置tabBar
- (void)setUpTabBar {
    //创建自定义的tabBar
    ANTabBar *tabBar = [[ANTabBar alloc] initWithFrame:self.tabBar.bounds];
     // 设置代理
    tabBar.delegate = self;
    // 给tabBar传递tabBarItem模型
    tabBar.items = self.items;
    // 添加自定义tabBar
    
    [self.tabBar addSubview:tabBar];
}

#pragma mark - 当点击tabBar上的按钮调用
- (void)tabBar:(ANTabBar *)tabBar didClickButton:(NSInteger)index
{
    if (index == 0 && self.selectedIndex == index) {
        //[_home refresh];
    }
    self.selectedIndex = index;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //移除系统的TABBARBUTTON
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
